package com.daocao.jy.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;

import com.daocao.jy.MainActivity;
import com.daocao.jy.R;
import com.daocao.jy.base.BaseActivity;
import com.daocao.jy.fragment.GuideFragment;
import com.daocao.jy.util.PreferenceUtils;
import com.daocao.jy.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {


    @BindView(R.id.pager)
    ViewPager pager;
    private Unbinder unbiner;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
        PreferenceUtils.putBoolean(this, "first_guide", false);
    }

    private void init() {
        Fragment[] fragments = new Fragment[4];
        for (int i = 0; i < 4; i++) {
            GuideFragment fragment = new GuideFragment();
            fragment.setPoi(i);
            fragments[i] = fragment;
        }
        pager.addOnPageChangeListener(this);
        pager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });
    }

    private int currentPage;

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        currentPage = position;
        if (position == 3) {
            pager.setOnTouchListener(new mTouchListener());
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private void startActivity() {
        try {

            Util.startActivity(GuideActivity.this, MainActivity.class);
            GuideActivity.this.finish();
            overridePendingTransition(R.anim.splash_in, R.anim.splash_out);

        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }

    class mTouchListener implements View.OnTouchListener {

        float x1 = 0;
        float x2 = 0;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (currentPage == 3) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //当手指按下的时候
                        x1 = event.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        //当手指离开的时候
                        x2 = event.getX();
                        if (x2 - x1 > 50) {
                            startActivity();
                        }
                        break;
                }
            }
            return false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }
}
